<template>
  <div>
      <Header/>
      <HomeSwiper :swiperList="swiperList"/>
      <HomeIcons :iconList="iconList"/>
      <HomeRecommend :recommendList="recommendList"/>
      <HomeWeekend :weekendList="weekendList"/>
  </div>
</template>
<script>

import Header from './components/Header.vue'
import HomeSwiper from './components/Swiper.vue'
import HomeIcons from './components/HomeIcons.vue'
import HomeRecommend from './components/Recommend.vue'
import HomeWeekend from './components/Weekend.vue'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  name: 'Home',
  components: { Header, HomeSwiper, HomeIcons, HomeRecommend, HomeWeekend },
  data () {
    return {
      oldArray: [1, 2, 3, 4, 5, 5, 4],
      lastCity: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: {
    ...mapState({CurrentCity: 'city'})
  },
  methods: {
    getNewArray () {
      const newArray = []
      for (let i = 0; i < this.oldArray.length; i++) {
        if (newArray.indexOf(this.oldArray[i]) === -1) {
          newArray.push(this.oldArray[i])
        }
      }
      console.log(newArray)
    },
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.CurrentCity)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.city = data.city
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted () {
    this.lastCity = this.CurrentCity
    this.getHomeInfo()
    this.getNewArray()
  },
  activated () {
    if (this.lastCity !== this.CurrentCity) {
      this.lastCity = this.CurrentCity
      this.getHomeInfo()
    }
  }
}
</script>
